!############################# Change Log ##################################
! 5.0.0
!
!###########################################################################
!  Copyright (C)  1990, 1995, 1999, 2000, 2003 - All Rights Reserved
!  Regional Atmospheric Modeling System - RAMS
!###########################################################################


subroutine rams_mem_alloc(proc_type)

  use mem_all
  use node_mod

  ! needed for Shallow Cumulus
  use mem_shcu
  use shcu_vars_const, only : nnshcu                           ! INTENT(IN)

  ! needed for Grell Cumulus parameterization
  use mem_grell_param       ! scalar parameters
  use mem_grell             ! main data
  use mem_scratch1_grell    ! scratch 1
  use mem_scratch2_grell    ! scratch 2
  use mem_scratch3_grell    ! scratch 3

  ! needed for SIB
  use sib_vars, only : N_CO2 ! INTENT(IN)
  use mem_sib_co2
  use mem_sib, only : sib_brams_g, sib_bramsm_g                 &
       , alloc_sib_brams, nullify_sib_brams, dealloc_sib_brams  &
       , filltab_sib_brams, zero_sib_brams

  ! Needed for Optmization in HTINT
  use mem_opt

  ! Needed for CATT
  use catt_start, only: CATT           ! intent(in)
  use mem_carma
  use mem_aerad, only: &
       nwave,          &           !INTENT(IN)
       initial_definitions_aerad !Subroutine
  use mem_globaer, only: &
       initial_definitions_globaer !Subroutine
  use mem_globrad, only: &
       initial_definitions_globrad !Subroutine
  use extras
  use mem_turb_scalar
  !25082004
  !  use mem_cutrans
  use mem_scratch2_grell_sh    ! scratch 2 shallow
  use mem_scratch3_grell_sh    ! scratch 3 shallow

  ! ALF
  ! Data for Optimization for vector machines
  use mem_micro_opt, only: &
       alloc_micro_opt

  ! For specific optimization depending the type of machine
  use machine_arq, only: machine ! INTENT(IN)

  ! Global grid dimension definitions
  use mem_grid_dim_defs, only: define_grid_dim_pointer ! subroutine

  use mem_scalar

  ! TEB_SPM
  use teb_spm_start, only: TEB_SPM ! INTENT(IN)

  use mem_emiss, only: isource ! INTENT(IN)

  use mem_gaspart, only: &
       gaspart_g,        & ! INTENT(IN)
       gaspartm_g,       & ! INTENT(IN)
       gaspart_vars,     & ! Type
       nullify_gaspart,  & ! Subroutine
       alloc_gaspart,    & ! Subroutine
       filltab_gaspart     ! Subroutine

  use mem_teb_common, only: &
       tebc_g,              & ! INTENT(IN)
       tebcm_g,             & ! INTENT(IN)
       nullify_tebc,        & ! Subroutine
       alloc_tebc,          & ! Subroutine
       filltab_tebc           ! Subroutine

  use teb_vars_const, only: iteb ! INTENT(IN)

  use mem_teb, only: &
       teb_g,        & ! INTENT(IN)
       tebm_g,       & ! INTENT(IN)
       nullify_teb,  & ! Subroutine
       alloc_teb,    & ! Subroutine
       filltab_teb     ! Subroutine


!--(DMK-CCATT)---------------------------------------------------------
 !srf - for CHEM 1
  use chem1_list, only:  &
       nspecies_chem=> nspecies, &
       PhotojMethod
  use mem_chem1
  use mem_plume_chem1 
  use aer1_list, only: &
       nspecies_aer=> nspecies
  use mem_aer1
  use carma_fastjx, only: &
       alloc_carma_fastjx
 !change MP - for CHEM1aq
  use chem1aq_list, only: &
       nspeciesaq_chem=> nspeciesaq
  use mem_chem1aq
  use mem_chemic
  use mem_stilt
  use mem_tuv, only : alloc_carma_tuv

!--(DMK-CCATT-END)---------------------------------------------------------


  implicit none

  ! Argumenst:
  integer, intent(in) :: proc_type

  ! Local Variables:
  integer, pointer :: nmzp(:),nmxp(:),nmyp(:)
  integer :: ng,nv,imean, nax
  ! Local variable to control Shallow Cumulus memory allocation
  integer :: Alloc_ShCu_Flag = 0
  ! Local variable to control Grell memory allocation
  integer :: Alloc_Grell_Flag = 0, ng_cp
  ! Local variables because of TEB_SPM
  type(gaspart_vars), pointer :: gaspart_p


  ! First, depending on type of process, define grid point pointers correctly..

  ! TEB_SPM
  nullify(gaspart_p)
  !

  if (proc_type == 0 .or. proc_type == 1) then
     !  This is the call for either a single processor run or
     !    for the master process
     nmzp => nnzp
     nmxp => nnxp
     nmyp => nnyp
  elseif (proc_type == 2) then
     !  This is the call for a initial compute node process
     nmzp => mmzp
     nmxp => mmxp
     nmyp => mmyp
  elseif (proc_type == 3) then
     !  This is the call for a dynamic balance compute node process
     nmzp => mmzp
     nmxp => mmxp
     nmyp => mmyp
     call dealloc_all()
  endif

  ! Call global grid dimension definitions
  call define_grid_dim_pointer(proc_type, ngrids, maxgrds, &
       nnzp, nnxp, nnyp, mmzp, mmxp, mmyp)

  !  If we are doing time-averaging for output, set flag ...
  imean=0
  if (avgtim /= 0.) imean=1

  ! Allocate universal variable tables

  allocate (num_var(maxgrds))
  allocate (vtab_r(maxvars,maxgrds))

  num_var(1:maxgrds)=0
  nvgrids=ngrids

  ! Allocate scalar table

  allocate(num_scalar(maxgrds))
  allocate(scalar_tab(maxsclr,maxgrds))
  num_scalar(1:maxgrds)=0

  ! Allocate Basic variables data type
  allocate(basic_g(ngrids),basicm_g(ngrids))
  do ng=1,ngrids
     call nullify_basic(basic_g(ng)) ; call nullify_basic(basicm_g(ng))
     call alloc_basic(basic_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     if (imean == 1) then
        call alloc_basic(basicm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     elseif (imean == 0) then
        call alloc_basic(basicm_g(ng),1,1,1,ng)
     endif

     call filltab_basic(basic_g(ng),basicm_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo

  ! Allocate Cuparm variables data type
  allocate(cuparm_g(ngrids),cuparmm_g(ngrids))

  ! For CATT
  if (CATT == 1) then
     allocate(cuparm_g_sh(ngrids),cuparmm_g_sh(ngrids))
  endif

  do ng=1,ngrids
     ! For CATT
     call nullify_cuparm(cuparm_g(ng))
     call nullify_cuparm(cuparmm_g(ng))

     if (CATT == 1) then
        call nullify_cuparm(cuparm_g_sh(ng))
        call nullify_cuparm(cuparmm_g_sh(ng))
     endif

     call nullify_cuparm(cuparm_g(ng)) ; call nullify_cuparm(cuparmm_g(ng))

     call alloc_cuparm(cuparm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)

     ! For CATT
     if (CATT == 1) then
        call alloc_cuparm(cuparm_g_sh(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     endif

     if (imean == 1) then

        call alloc_cuparm(cuparmm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)

        ! For CATT
        if (CATT == 1) then
           call alloc_cuparm(cuparmm_g_sh(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
        endif
     elseif (imean == 0) then

        call alloc_cuparm(cuparmm_g(ng),1,1,1,ng)

        ! For CATT
        if (CATT == 1) then
           call alloc_cuparm(cuparmm_g_sh(ng),1,1,1,ng)
        endif

     endif

     call filltab_cuparm(cuparm_g(ng),cuparmm_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)

     ! For CATT
     if (CATT == 1) then
        call filltab_cuparm_sh(cuparm_g_sh(ng),cuparmm_g_sh(ng),imean  &
             ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
     endif

  enddo

  ! Allocate Leaf type

  allocate(leaf_g(ngrids),leafm_g(ngrids))
  do ng=1,ngrids
     call nullify_leaf(leaf_g(ng)) ; call nullify_leaf(leafm_g(ng))
     call alloc_leaf(leaf_g(ng),nmzp(ng),nmxp(ng),nmyp(ng)  &
          ,nzg,nzs,npatch,ng)
     if (imean == 1) then
        call alloc_leaf(leafm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng)  &
             ,nzg,nzs,npatch,ng)
     elseif (imean == 0) then
        call alloc_leaf(leafm_g(ng),1,1,1,1,1,1,1)
     endif

     call filltab_leaf(leaf_g(ng),leafm_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),nzg,nzs,npatch,ng)
  enddo
  ! Bob (1/10/2002) added the following line.  Is this the right place for
  ! the long term??
  call alloc_leafcol(nzg,nzs)


  ! Allocate Micro variables data type
  allocate(micro_g(ngrids),microm_g(ngrids))
  do ng=1,ngrids
     call nullify_micro(micro_g(ng)) ; call nullify_micro(microm_g(ng))
     call alloc_micro(micro_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     if (imean == 1) then
        call alloc_micro(microm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     elseif (imean == 0) then
        call alloc_micro(microm_g(ng),1,1,1,ng)
     endif

     call filltab_micro(micro_g(ng),microm_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo


  ! Allocate Optimized Micro variables
  ! Only for use with SX-6 specific optimization
  if (machine==1) then
     call alloc_micro_opt(nmzp,nmxp,nmyp)
  endif

  ! Allocate radiate variables data type
  allocate(radiate_g(ngrids),radiatem_g(ngrids))
  do ng=1,ngrids
     call nullify_radiate(radiate_g(ng)) ; call nullify_radiate(radiatem_g(ng))
     call alloc_radiate(radiate_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     if (imean == 1) then
        call alloc_radiate(radiatem_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     elseif (imean == 0) then
        call alloc_radiate(radiatem_g(ng),1,1,1,ng)
     endif

     call filltab_radiate(radiate_g(ng),radiatem_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo


  ! Allocate turb variables data type
  allocate(turb_g(ngrids),turbm_g(ngrids))
  do ng=1,ngrids
     call nullify_turb(turb_g(ng)) ; call nullify_turb(turbm_g(ng))
     call alloc_turb(turb_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     if (imean == 1) then
        call alloc_turb(turbm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     elseif (imean == 0) then
        call alloc_turb(turbm_g(ng),1,1,1,ng)
     endif

     call filltab_turb(turb_g(ng),turbm_g(ng),imean  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo


  ! Allocate varinit variables data type.
  !    These do not need "mean" type ever.
  allocate(varinit_g(ngrids),varinitm_g(ngrids))
  do ng=1,ngrids
     call nullify_varinit(varinit_g(ng)) ; call nullify_varinit(varinitm_g(ng))
     call alloc_varinit(varinit_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
     call alloc_varinit(varinitm_g(ng),1,1,1,ng)

     call filltab_varinit(varinit_g(ng),varinitm_g(ng),0  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo


  ! Allocate oda variables data type.
  !    These do not need "mean" type ever.
  allocate(oda_g(ngrids),odam_g(ngrids))
  do ng=1,ngrids

     call nullify_oda(oda_g(ng)) ; call nullify_oda(odam_g(ng))

     call alloc_oda(oda_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng,proc_type)
     call alloc_oda(odam_g(ng),1,1,1,ng,proc_type)

     ! ----------

     call filltab_oda(oda_g(ng),odam_g(ng),0  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)

  enddo


  ! Allocate grid variables data type.

  allocate(grid_g(ngrids),gridm_g(ngrids))
  do ng=1,ngrids
     call nullify_grid(grid_g(ng)) ; call nullify_grid(gridm_g(ng))
     call alloc_grid(grid_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng,if_adap)
     call alloc_grid(gridm_g(ng),1,1,1,ng,if_adap)

     call filltab_grid(grid_g(ng),gridm_g(ng),0  &
          ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
  enddo


  ! Allocate any added Scalar types
  ! NOT ALLOWING DIFFERENT NUMBERS OF SCALARS ON DIFFERENT NESTS


  !   Allocate length 1 of these datatypes by default
  allocate(scalar_g(1,ngrids),scalarm_g(1,ngrids))

  if (naddsc > 0) then
     ! deallocate datatypes, then re-alloc to correct length
     deallocate(scalar_g,scalarm_g)
     allocate(scalar_g(naddsc,ngrids),scalarm_g(naddsc,ngrids))
     do ng=1,ngrids
        call nullify_scalar(scalar_g(:,ng),naddsc)
        call nullify_scalar(scalarm_g(:,ng),naddsc)
        call alloc_scalar(scalar_g(:,ng),nmzp(ng),nmxp(ng),nmyp(ng)  &
             ,naddsc)
        if (imean == 1) then
           call alloc_scalar(scalarm_g(:,ng),nmzp(ng),nmxp(ng),nmyp(ng)  &
                ,naddsc)
        elseif (imean == 0) then
           call alloc_scalar(scalarm_g(:,ng),1,1,1,naddsc)
        endif

     enddo
     ! For CATT
     do ng=1,ngrids
        do nax=1,naddsc ! For CATT
           call filltab_scalar(scalar_g(nax,ng),scalarm_g(nax,ng),imean  &
                ,nmzp(ng),nmxp(ng),nmyp(ng),ng,nax)
        end do
     enddo

  else     ! necessary for CARMA Radiation, assume naddsc==3


!--(DMK-CCATT)---------------------------------------------------------
!- 04/04/2007
!srf- chem: this is not necessary anymore for catt-chemistry
!--(DMK-original)------------------------------------------------------
! deallocate datatypes, then re-alloc to correct length
!     deallocate(scalar_g,scalarm_g)
!     allocate(scalar_g(3,ngrids),scalarm_g(3,ngrids))
!     do ng=1,ngrids
!        call nullify_scalar(scalar_g(:,ng), 3)
!        call nullify_scalar(scalarm_g(:,ng), 3)
!        ! assume grid 1x1xz
!        call alloc_scalar(scalar_g(:,ng),  1, 1, 1, 3)
!        call alloc_scalar(scalarm_g(:,ng), 1, 1, 1, 3)
!        !do na=1, 3
!        !call filltab_scalar(scalar_g(na,ng), scalarm_g(na,ng), imean  &
!        !     , 1, 1, 1, ng, na)
!        !enddo
!     enddo
!--(DMK-CCATT-END)-----------------------------------------------------
  endif


  ! TEB_SPM
  if (TEB_SPM==1) then
     if(isource==1)then
        !-----------------------------------------------------------------------
        ! Allocate  gaspart vars for emission
        !
        ! Defining pointers
        allocate(gaspart_g(ngrids),gaspartm_g(ngrids))
        do ng=1,ngrids
           call nullify_gaspart(gaspart_g(ng))
           call nullify_gaspart(gaspartm_g(ng))
           call alloc_gaspart(gaspart_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           if (imean == 1) then
              call alloc_gaspart(gaspartm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           elseif (imean == 0) then
              call alloc_gaspart(gaspartm_g(ng),1,1,1,ng)
           endif

           call filltab_gaspart(gaspart_g(ng),gaspartm_g(ng),imean  &
                ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
        enddo
     endif
  endif


  ! Allocate Tendency data type,  filltab_tendency is responsible
  !   for filling the main atmospheric model variables in the scalar table,
  !   so make sure to call any routines that define scalar variables first.

  ! Assuming same scalars on all grids!!!!!

  call nullify_tend(naddsc)

  call alloc_tend(nmzp,nmxp,nmyp,ngrids,naddsc,proc_type)



!--(DMK-CCATT)---------------------------------------------------------
!srf ----------------------------------------------------- for CHEM 1 
  if (CATT == 1  .and. CHEMISTRY >= 0) then
    
      call define_n_dyn_chem(ngrids,dtlong,nndtrat)
    
      allocate(chem1_g    (     nspecies_chem,ngrids),chem1m_g    (     nspecies_chem,ngrids))
      allocate(chem1_src_g (max_ntimes_src,nsrc,nspecies_chem,ngrids),&
           chem1m_src_g(max_ntimes_src,nsrc,nspecies_chem,ngrids))
  
     do ng=1,ngrids
        call define_chem1_src_zdim(chem1_src_z_dim_g(:,ng),nmzp(ng))   
        call nullify_chem1(chem1_g (:,ng),chem1_src_g (:,:,:,ng), nspecies_chem) 
        call nullify_chem1(chem1m_g(:,ng),chem1m_src_g(:,:,:,ng), nspecies_chem)
     
        call alloc_chem1(chem1_g(:,ng),chem1_src_g(:,:,:,ng),chem1_src_z_dim_g(:,ng) &
                     ,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem, ng) 
     
        if (imean == 1) then  
           call alloc_chem1(chem1m_g(:,ng),chem1m_src_g(:,:,:,ng),chem1_src_z_dim_g(:,ng) &
           		   ,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem, ng)
        
        elseif (imean == 0) then
           call alloc_chem1(chem1m_g(:,ng),chem1m_src_g(:,:,:,ng),chem1_src_z_dim_g(:,ng) &
        		      ,1,1,1,nspecies_chem, ng)
        endif
        
        call filltab_chem1(chem1_g     (:,ng)	 ,chem1m_g    (:,ng)	  &
        		  ,chem1_src_g (:,:,:,ng),chem1m_src_g(:,:,:,ng)  &
        		  ,imean ,chem1_src_z_dim_g(:,ng) &
        		  ,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem,ng) 
     end do

     call nullify_tend_chem1(nspecies_chem)

     call alloc_tend_chem1(nmzp,nmxp,nmyp,ngrids,nspecies_chem,proc_type)
 
     !-- plumerise  section 
     allocate(plume_g     (nveg_agreg,nspecies_chem,ngrids),plumem_g     (nveg_agreg,nspecies_chem,ngrids))
     allocate(plume_mean_g(nveg_agreg,ngrids)              ,plume_meanm_g(nveg_agreg,ngrids))

     do ng=1,ngrids

        call nullify_plume_chem1(plume_g  (:,:,ng),plume_mean_g  (:,ng),nspecies_chem) 
        call nullify_plume_chem1(plumem_g (:,:,ng),plume_meanm_g (:,ng),nspecies_chem)       
        call alloc_plume_chem1(plume_g(:,:,ng),plume_mean_g  (:,ng)&
             ,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem) 
        if (imean == 1) then  
           
           call alloc_plume_chem1(plumem_g(:,:,ng),plume_meanm_g (:,ng)&
                ,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem)     
        elseif (imean == 0) then
        
           call alloc_plume_chem1(plumem_g(:,:,ng),plume_meanm_g (:,ng),1,1,1,nspecies_chem)
        endif
      
        call filltab_plume_chem1(plume_g   (:,:,ng),plumem_g    (:,:,ng)&
             ,plume_mean_g(:,ng)  ,plume_meanm_g (:,ng)  &
             ,imean,nmzp(ng),nmxp(ng),nmyp(ng),nspecies_chem,ng) 
     enddo
  
     !-- aerosol section
     if( AEROSOL == 1) then

        allocate(aer1_g(nmodes,nspecies_aer,ngrids),aer1m_g(nmodes,nspecies_aer,ngrids))
 
        do ng=1,ngrids
           call define_aer1_src_zdim(aer1_src_z_dim_g(:,ng),nmzp(ng))
    	 
           call nullify_aer1(aer1_g (:,:,ng),nmodes, nspecies_aer) 
           call nullify_aer1(aer1m_g(:,:,ng),nmodes, nspecies_aer)
    	 
           call alloc_aer1(aer1_g(:,:,ng),chem1_src_z_dim_g(:,ng) &
                ,nmzp(ng),nmxp(ng),nmyp(ng),nmodes,nspecies_aer) 
    	 
           if (imean == 1) then  
              call alloc_aer1(aer1m_g(:,:,ng),chem1_src_z_dim_g(:,ng) &
    			    ,nmzp(ng),nmxp(ng),nmyp(ng),nmodes,nspecies_aer)
    	 
           elseif (imean == 0) then
              call alloc_aer1(aer1m_g(:,:,ng),aer1_src_z_dim_g(:,ng) &
    			    ,1,1,1,nmodes,nspecies_aer)
           endif
    	 
           call filltab_aer1(aer1_g(:,:,ng)	,aer1m_g(:,:,ng)       &
                ,imean ,aer1_src_z_dim_g(:,ng) &
                ,nmzp(ng),nmxp(ng),nmyp(ng),nmodes,nspecies_aer,ng) 
  
        enddo

        call nullify_tend_aer1(nmodes,nspecies_aer)

        call alloc_tend_aer1(nmzp,nmxp,nmyp,ngrids,nmodes,nspecies_aer,proc_type)
     endif
     !-- CHEM1AQ section
     !change MP---------------------------------------------- for CHEM1aq 
     if (CHEMISTRY_AQ > 0) then
        allocate(chem1aq_g  (  nspeciesaq_chem,ngrids),chem1maq_g    (  nspeciesaq_chem,ngrids))
        allocate(chemic_g(ngrids)) 

        do ng=1,ngrids
   	 
           call nullify_chem1aq(chem1aq_g (:,ng), nspeciesaq_chem) 
           call nullify_chem1aq(chem1maq_g(:,ng), nspeciesaq_chem)
   	   call nullify_chemic(chemic_g(ng))
   	 
           call alloc_chem1aq(chem1aq_g(:,ng) &
                ,nmzp(ng),nmxp(ng),nmyp(ng),nspeciesaq_chem) 
          
	   call alloc_chemic(chemic_g(ng),nmzp(ng),nmxp(ng),nmyp(ng))
  	 
           if (imean == 1) then  
              call alloc_chem1aq(chem1maq_g(:,ng) &
                   ,nmzp(ng),nmxp(ng),nmyp(ng),nspeciesaq_chem)
 	 
           elseif (imean == 0) then
              call alloc_chem1aq(chem1maq_g(:,ng) &
                   ,1,1,1,nspeciesaq_chem)
           endif
   	 
           call filltab_chem1aq(chem1aq_g(:,ng)	    ,chem1maq_g(:,ng)	    &
                ,imean,nmzp(ng),nmxp(ng),nmyp(ng),nspeciesaq_chem,ng) 
        enddo

        call nullify_tend_chem1aq(nspeciesaq_chem)

        call alloc_tend_chem1aq(nmzp,nmxp,nmyp,ngrids,nspeciesaq_chem,proc_type) 
     endif
     !end change MP--------------------------------------------fo CHEM1 aq - end
  
  endif
!srf ------------------------------------------------ for CHEM 1  - end
!--(DMK-CCATT-END)-----------------------------------------------------



  do ng=1,ngrids
     ! TEB_SPM
     if (TEB_SPM==1) then
        nullify(gaspart_p)
        gaspart_p => gaspart_g(ng)
     endif
     !

     call filltab_tend(basic_g(ng), micro_g(ng), turb_g(ng),  &
          scalar_g(:,ng),                                     &
          ! TEB_SPM
          !!gaspart_g(ng),                                      &
          gaspart_p,                                          &
          !
          naddsc, ng)


!--(DMK-CCATT)---------------------------------------------------------
     !-srf-chem1
     if (CATT == 1  .and. CHEMISTRY >= 0)  then 
         
	 call filltab_tend_chem1(nspecies_chem,ng)
	 
	 !change MP ---chem1aq
         if(CHEMISTRY_AQ >= 1) call filltab_tend_chem1aq(nspeciesaq_chem,ng)
         !end change MP --chem1aq- END     


	 if(AEROSOL == 1)     call filltab_tend_aer1(nmodes,nspecies_aer,ng)
         	 
     endif
     !-srf-chem1-END
!--(DMK-CCATT-END)-----------------------------------------------------

  enddo

  ! Allocate Scratch data type, This also fills the max's that are needed
  !    by nesting stuff.
  call nullify_scratch()

  call alloc_scratch(nmzp,nmxp,nmyp,nnzp,nnxp,nnyp,ngrids  &
       ,nzg,nzs,npatch,proc_type,maxnxp,maxnyp,maxnzp)

  call filltab_scratch()


  ! For CATT - LFR
  if (CATT == 1) then

     allocate(turb_s(ngrids))

     do ng=1,ngrids

        call nullify_turb_s(turb_s(ng))

        call alloc_turb_s(turb_s(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)

        call filltab_turb_s(turb_s(ng)           &
             ,nmzp(ng),nmxp(ng),nmyp(ng),ng)

     enddo

  endif


  ! Reproducibility - Saulo Barros
  call nullify_scratch1()
  call alloc_scratch1(nodebounds,maxgrds,ngrids,mmzp,mynum)

  ! For optmization - ALF
  call nullify_opt_scratch()

  if ((if_adap==0).and.(ihorgrad==2)) then

     call alloc_opt_scratch(proc_type,ngrids,nnzp,nnxp,nnyp,1000,1000)

  endif

  ! Allocate nested boundary interpolation arrays. All grids will be allocated.

!!$  if (proc_type == 0 .or. proc_type == 2) then
  ! Changed by Alvaro L.Fazenda
  ! To correct a problem when running in a NEC SX-6
  ! Master process needs allocation for nesting in a parallel run
  if (proc_type == 0 .or. proc_type == 2 .or. proc_type == 1) then
     do ng=1,ngrids
        if(nxtnest(ng) == 0 ) then
           call alloc_nestb(ng,1,1,1)
        else
           call alloc_nestb(ng,nnxp(ng),nnyp(ng),nnzp(ng))
        endif
     enddo
  endif

  !--------------------------------------------------------------------------
  ! Allocate data for Shallow Cumulus
  !
  ! Verifying if the allocation is necessary in any grids
  do ng=1, ngrids
     if (NNSHCU(ng) == 1) Alloc_ShCu_Flag = 1
  enddo
  !Alloc_ShCu_Flag = 1  !!Provisorio
  if (Alloc_ShCu_Flag == 1) then
     allocate(shcu_g(ngrids),shcum_g(ngrids))
     do ng=1,ngrids
        call nullify_shcu(shcu_g(ng))
        call nullify_shcu(shcum_g(ng))
        call alloc_shcu(shcu_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
        if (imean == 1) then
           call alloc_shcu(shcum_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
        elseif (imean == 0) then
           call alloc_shcu(shcum_g(ng),1,1,1,ng)
        endif

        call filltab_shcu(shcu_g(ng),shcum_g(ng),imean  &
             ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
     enddo
  endif
  !--------------------------------------------------------------------------


  !--------------------------------------------------------------------------
  ! Allocate data for Grell Cumulus
  !
  ! Verifying if the allocation is necessary in any grids
  do ng=1, ngrids
     if (NNQPARM(ng) == 2) Alloc_Grell_Flag = 1
  enddo
  ! If (Alloc_Grell_Flag == 1) start allocation
  if (Alloc_Grell_Flag == 1) then
     ! Calculating the necessary space for scratch data
     call define_memory(nmxp, nmyp, nmzp, ngrids, nnqparm)
     ! Allocating data for scratch data
     call alloc_scratch1_grell

     call zero_scratch1_grell

     call alloc_scratch2_grell
     call alloc_scratch3_grell

     if (CATT == 1) then
        call alloc_scratch2_grell_sh ! For CATT - LFR
        call alloc_scratch3_grell_sh ! For CATT - LFR
     endif

     ! Allocating data for main Grell data
     ! Allocate to a quantity set by ngrids_cp not ngrids
     allocate(grell_g(ngrids_cp),grellm_g(ngrids_cp))
     ! For CATT
     if (CATT == 1) then
        allocate(grell_g_sh(ngrids_cp),grellm_g_sh(ngrids_cp))
     endif

     ng_cp = 1
     do ng=1,ngrids
        if (NNQPARM(ng) == 2) then
           call nullify_grell(grell_g(ng_cp))
           call nullify_grell(grellm_g(ng_cp))

           !For CATT
           if (CATT == 1) then
              call nullify_grell(grell_g_sh(ng_cp))
              call nullify_grell(grellm_g_sh(ng_cp))
           endif

           ! For CATT
           call alloc_grell(grell_g(ng_cp),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           if (CATT == 1) then
              call alloc_grell(grell_g_sh(ng_cp),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           endif

           if (imean == 1) then
              ! For CATT
              call alloc_grell(grellm_g(ng_cp),nmzp(ng),nmxp(ng),nmyp(ng),ng)
              if (CATT == 1) then
                 call alloc_grell(grellm_g_sh(ng_cp),  &
                      nmzp(ng),nmxp(ng),nmyp(ng),ng)
              endif

           elseif (imean == 0) then

              ! For CATT
              call alloc_grell(grellm_g(ng_cp),1,1,1,ng)
              if (CATT == 1) then
                 call alloc_grell(grellm_g_sh(ng_cp),1,1,1,ng)
              endif

           endif

           ! For CATT
           call filltab_grell(grell_g(ng_cp),grellm_g(ng_cp),imean  &
                ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
           if (CATT == 1) then
              call filltab_grell_sh(grell_g_sh(ng_cp),grellm_g(ng_cp),imean  &
                   ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
           endif

           ng_cp = ng_cp + 1
        endif
     enddo

     ! For CATT
     call zero_scratch2_grell() !LFR
     call zero_scratch3_grell() !LFR
     if (CATT == 1) then
        call Zero_scratch2_grell_sh() !LFR
        call zero_scratch3_grell_sh() !LFR
     endif

     Flag_Grell = 2
     ! For New Grell Param.
     if     (CLOSURE_TYPE == 'EN') then
        icoic = 0
     elseif (CLOSURE_TYPE == 'GR') then
        icoic = 1
     elseif (CLOSURE_TYPE == 'LO') then
        icoic = 4
     elseif (CLOSURE_TYPE == 'MC') then
        icoic = 7
     elseif (CLOSURE_TYPE == 'SC') then
        icoic = 10
     elseif (CLOSURE_TYPE == 'AS') then
        icoic = 13
     else
        print *, "****Grell Closure type ERROR!!"
        ! the subroutine opspec3 stop the program before this point.
     endif

     ! For CATT
     if (CATT == 1) then
        icoic_sh=icoic !LFR
     endif

  endif

  ! Allocation for SiB
  if (ISFCL == 3) then
     allocate(sib_g(ngrids, N_CO2), sibm_g(ngrids, N_CO2))
     allocate(sib_brams_g(ngrids), sib_bramsm_g(ngrids))
     do ng=1,ngrids
        ! SiB CO2
        call nullify_sib_co2(sib_g(ng, 1))
        call nullify_sib_co2(sibm_g(ng, 1))
        call alloc_sib_co2(sib_g(ng, 1), nmxp(ng), nmyp(ng))
        ! Putting zero on all values
        call zero_sib_co2(sib_g(ng, 1), nmxp(ng), nmyp(ng))
        if (imean == 1) then
           call alloc_sib_co2(sibm_g(ng, 1), nmxp(ng), nmyp(ng))
           ! Putting zero on all values
           call zero_sib_co2(sibm_g(ng, 1), nmxp(ng), nmyp(ng))
        elseif (imean == 0) then
           call alloc_sib_co2(sibm_g(ng, 1), 1, 1)
           ! Putting zero on all values
           call zero_sib_co2(sibm_g(ng, 1), 1, 1)
        endif
        call filltab_sib_co2(sib_g(ng, 1), sibm_g(ng, 1), imean  &
             , nmxp(ng), nmyp(ng), ng)
        ! Putting zero on all values
        ! SiB BRAMS types
        call nullify_sib_brams(sib_brams_g(ng))
        call nullify_sib_brams(sib_bramsm_g(ng))
        call alloc_sib_brams(sib_brams_g(ng), nmxp(ng), nmyp(ng))
        ! Putting zero on all values
        call zero_sib_brams(sib_brams_g(ng), nmxp(ng), nmyp(ng))
        if (imean == 1) then
           call alloc_sib_brams(sib_bramsm_g(ng), nmxp(ng), nmyp(ng))
           ! Putting zero on all values
           call zero_sib_brams(sib_bramsm_g(ng), nmxp(ng), nmyp(ng))
        elseif (imean == 0) then
           call alloc_sib_brams(sib_bramsm_g(ng), 1, 1)
           ! Putting zero on all values
           call zero_sib_brams(sib_bramsm_g(ng), 1, 1)
        endif
        call filltab_sib_brams(sib_brams_g(ng), sib_bramsm_g(ng), imean  &
             , nmxp(ng), nmyp(ng), ng)

     enddo
  endif

!--(DMK-CCATT)---------------------------------------------------------
  ![ML
  !Allocate stilt variables data type
  !print*, 'start stilt alloc'
  allocate(stilt_g(ngrids), stiltm_g(ngrids))
  do ng=1, ngrids
    call nullify_stilt(stilt_g(ng)); call nullify_stilt(stiltm_g(ng))
    call alloc_stilt(stilt_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
    if (imean == 1) then
      call alloc_stilt(stiltm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
    else if (imean == 0) then
      call alloc_stilt(stiltm_g(ng),1,1,1,ng)
    end if  
    call filltab_stilt(stilt_g(ng),stiltm_g(ng),imean, &
                       nmzp(ng),nmxp(ng),nmyp(ng),ng)
  end do
  !ML]
!--(DMK-CCATT-END)-----------------------------------------------------


  ! CATT - Allocation for Transporte
  if (CATT == 1) then
     allocate(extra2d (na_extra2d,ngrids))
     allocate(extra3d (na_extra3d,ngrids))
     allocate(extra2dm(na_extra2d,ngrids))
     allocate(extra3dm(na_extra3d,ngrids))
     call nullify_extra2d(extra2d,na_extra2d,ngrids)
     call nullify_extra2d(extra2dm,na_extra2d,ngrids)
     call nullify_extra3d(extra3d,na_extra3d,ngrids)
     call nullify_extra3d(extra3dm,na_extra3d,ngrids)
     do ng=1,ngrids
        call alloc_extra2d(extra2d,nmxp(ng),nmyp(ng),na_extra2d,ng)
        call zero_extra2d(extra2d,na_extra2d,ng)
        if (imean == 1) then
           call alloc_extra2d(extra2dm,nmxp(ng),nmyp(ng),na_extra2d,ng)
           call zero_extra2d(extra2dm,na_extra2d,ng)
        else
           call alloc_extra2d(extra2dm,1,1,na_extra2d,ng)
           call zero_extra2d(extra2dm,na_extra2d,ng)
        end if
        call alloc_extra3d(extra3d,nmzp(ng),nmxp(ng),nmyp(ng),na_extra3d,ng)
        call zero_extra3d(extra3d,na_extra3d,ng)
        if (imean == 1) then
           call alloc_extra3d(extra3dm,  &
                nmzp(ng),nmxp(ng),nmyp(ng),na_extra3d,ng)
           call zero_extra3d(extra3dm,na_extra3d,ng)
        else
           call alloc_extra3d(extra3dm,1,1,1,na_extra3d,ng)
           call zero_extra3d(extra3dm,na_extra3d,ng)
        end if
     end do
     do ng=1,ngrids
        do nax=1,na_extra2d
           call filltab_extra2d(extra2d(nax,ng),extra2dm(nax,ng),imean, &
                nmxp(ng),nmyp(ng),ng,nax)
        end do
        do nax=1,na_extra3d
           call filltab_extra3d(extra3d(nax,ng),extra3dm(nax,ng),imean, &
                nmzp(ng),nmxp(ng),nmyp(ng),ng,nax)
        end do
     end do
  endif


  ! Carma Radiation
  if (ilwrtyp==4 .or. iswrtyp==4) then
     call initial_definitions_aerad()
     call initial_definitions_globrad()
     call initial_definitions_globaer()

!--(DMK-CCATT)---------------------------------------------------------
     if (trim(PhotojMethod) == 'FAST-JX') &
	  CALL alloc_carma_fastjx(maxval(nmzp(1:ngrids))&
          ,maxval(nmxp(1:ngrids))&
          ,maxval(nmyp(1:ngrids)))
     if (trim(PhotojMethod) == 'FAST-TUV' .and. CHEMISTRY > 0) &
          call alloc_carma_tuv()
!--(DMK-CCATT-END)-----------------------------------------------------
     

     allocate(carma(ngrids))
     allocate(carma_m(ngrids))
     do ng=1,ngrids
        call nullify_carma(carma,ng)
        call alloc_carma(carma,ng,nmxp(ng),nmyp(ng),nwave)
        call zero_carma(carma,ng)
        call nullify_carma(carma_m,ng)
        if(imean == 1) then
           call alloc_carma(carma_m,ng,nmxp(ng),nmyp(ng),nwave)
           call zero_carma(carma_m,ng)
        else
           call alloc_carma(carma_m,ng,1,1,nwave)
           call zero_carma(carma_m,ng)
        end if

        call filltab_carma(carma(ng),carma_m(ng),ng,imean,  &
             nmxp(ng),nmyp(ng),nwave)
     end do
  endif

  if (TEB_SPM==1) then
     !---------------------------------------------------------------------
     ! Allocate common use variables (TEB_SPM,LEAF)
     !
     allocate(tebc_g(ngrids),tebcm_g(ngrids))
     do ng=1,ngrids
        call nullify_tebc(tebc_g(ng))
        call nullify_tebc(tebcm_g(ng))
        call alloc_tebc(tebc_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
        if (imean == 1) then
           call alloc_tebc(tebcm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
        elseif (imean == 0) then
           call alloc_tebc(tebcm_g(ng),1,1,1,ng)
        endif

        call filltab_tebc(tebc_g(ng),tebcm_g(ng),imean  &
             ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
     enddo

     !---------------------------------------------------------------------
     ! Allocate data for urban canopy parameterization
     !
     if(iteb==1)then
        allocate(teb_g(ngrids),tebm_g(ngrids))
        do ng=1,ngrids
           call nullify_teb(teb_g(ng))
           call nullify_teb(tebm_g(ng))
           call alloc_teb(teb_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           if (imean == 1) then
              call alloc_teb(tebm_g(ng),nmzp(ng),nmxp(ng),nmyp(ng),ng)
           elseif (imean == 0) then
              call alloc_teb(tebm_g(ng),1,1,1,ng)
           endif

           call filltab_teb(teb_g(ng),tebm_g(ng),imean  &
                ,nmzp(ng),nmxp(ng),nmyp(ng),ng)
        enddo
     endif
  endif


  !--------------------------------------------------------------------------

  ! Set "Lite" variable flags according to namelist input LITE_VARS.
  if (proc_type == 0 .or. proc_type == 2 .or. proc_type == 1) then
     call lite_varset(proc_type)
  endif


  ! Set ALL variables in the vtab_r variable table to zero by default. These
  !  are variables processed in the filltab_* routines with a call to vtables2.
  !  This does NOT include scratch arrays, tendencies, or mean arrays.
  do ng = 1, ngrids
     do nv = 1,num_var(ng)
        call azero( vtab_r(nv,ng)%npts, vtab_r(nv,ng)%var_p)
     enddo
  enddo

end subroutine rams_mem_alloc
